<template>
  <div class="search">
    <n-card :bordered="false" class="mt-4 proCard">
      <n-grid cols="1" :x-gap="12">
        <n-gi>
          <n-form
            :model="queryForm"
            ref="queryFormRef"
            label-placement="left"
            inline>
            <n-grid :x-gap="12"  :cols="4" responsive="screen" @keydown.enter.native="handleSearch">
              <n-gi>
                <n-form-item label="日志类型" path="type">
                  <n-select placeholder="请选择日志类型" v-model:value="queryForm.type" :options="logTypeList" />
                </n-form-item>
              </n-gi>
              <n-gi>
                <n-form-item style="margin-right: 0" class="br">
                  <n-space>
                    <n-button type="primary" @click="handleSearch">
                      <template #icon>
                        <n-icon>
                          <IosSearch/>
                        </n-icon>
                      </template>查询</n-button>
                    <n-button @click="handleReset">
                      重置</n-button>
                  </n-space>
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
          <n-grid :x-gap="12" :cols="2" class="operation" style="margin-bottom: 1vh" >
            <n-gi>
              <n-space>
                <n-button type="error" @click="deleteAll">
                  <template #icon>
                    <n-icon>
                      <IosTrash/>
                    </n-icon>
                  </template>
                  删除</n-button>
              </n-space>
            </n-gi>
          </n-grid>
          <n-grid :cols="1" style="margin-bottom: 1vh;">
            <n-gi :span="1">
              <n-alert type="info" size="small">
                已选择
                <span class="select-count">{{selectedKeys.length}}</span> 项
                <a class="select-clear" @click="clearSelect" v-if="selectedKeys.length>0">清空</a>
              </n-alert>
            </n-gi>
          </n-grid>
          <n-grid :cols="1">
            <n-gi>
              <n-data-table
                ref="table"
                :columns="column"
                :pagination="pagination"
                :remote="true"
                :loading="loading"
                :data="data"
                :row-key="row => row.id"
                @update:checked-row-keys="handleCheck"
                @update:page-size="changePageSize"
                @update:page="changePage"
                :checked-row-keys="selectedKeys"
              >
              </n-data-table>
            </n-gi>
          </n-grid>
        </n-gi>
      </n-grid>
    </n-card>
  </div>
</template>

<script>
import {defineComponent,ref,h,onMounted,reactive} from 'vue'
import {IosSearch,IosAdd,IosTrash} from '@vicons/ionicons4'
import {useMessage,NButton,NTag,useDialog} from 'naive-ui'
import {logList,deleteLog} from "../../../api/system/log";

export default defineComponent({
  name: "Log",
  components:{
    IosSearch,
    IosAdd,
    IosTrash,
    NTag
  },
  setup(){
    const message=useMessage();
    const dialog=useDialog();
    let initQueryForm=()=>{
      return  {
        page:1,
        pageSize:10,
        type:null,
      }
    };
    const logTypeList=[
      {label:'操作记录',value:1},
      {label:'异常记录',value:2}
    ];
    const queryForm=ref(initQueryForm());
    const selectedKeys=ref([]);
    const data=ref([]);
    const loading=ref(false);
    const pagination=reactive({
      itemCount:null,
      prefix ({ itemCount }) {
        return `共 ${itemCount} 项`
      },
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10,20,30,40,50],
    });
    const column =ref([
      {
        type: "selection",
        width: 60,
        align: "center",
        fixed: "left"
      },
      {
        type: "index",
        width: 60,
        align: "center",
        fixed: "left"
      },
      {
        title:'请求ip',
        key:'requestIp',
        align: 'center',
      },
      {
        title:'请求地点',
        key:'location',
        align: 'center',
      },
      {
        title: '日志类型',
        align: 'center',
        key: 'type',
        render:function (row){
          if (row.type ==1) {
            return h("div", [
              h(NTag,
                {
                  type: 'success',
                },
                { default: () => '正常记录' }
              )
            ]);
          } else if (row.type==2) {
            return h("div", [
              h(NTag,
                {
                  type: 'error',
                },
                { default: () => '异常记录' }
              )
            ]);
          }
        }
      },
      {
        title: '操作类型',
        align: 'center',
        key: 'operateType',
        render:function (row){
          if (row.type ==1) {
            return h("div", [
              h(NTag,
                {
                  type: 'success',
                },
                { default: () => '操作日志' }
              )
            ]);
          } else if (row.type==0) {
            return h("div", [
              h(NTag,
                {
                  type: 'success',
                },
                { default: () => '登录日志' }
              )
            ]);
          }
        }
      },
      {
        title: '描述',
        align: 'center',
        key: 'description',
      },
      {
        title: '操作用户',
        align: 'center',
        key: 'userName',
      },
      {
        title: '请求URL',
        align: 'center',
        key: 'actionUrl',
        sortable: true,
      },
      {
        title: "调用开始时间",
        key: "startTime",
        sortable: true,
        align: 'center',
        minWidth: 150,
      },
      {
        title: "调用结束时间",
        key: "finishTime",
        sortable: true,
        align: 'center',
        minWidth: 150,
      },
      // {
      //   title: '操作',
      //   key: 'actions',
      //   align:'center',
      //   render: function (row) {
      //     return h("div", [
      //       h(
      //         NButton,
      //         {
      //           type: "error",
      //           size: "small",
      //           onClick: () => handleDelete(row)
      //         },
      //         {default: () => '删除'}
      //       )
      //     ]);
      //   }
      // }
    ]);
    function handleDelete(row){
      selectedKeys.value.push(row.id);
      deleteAll();
    }
    const handleSearch=()=>{
      initLogList();
    };
    const  handleReset=()=>{
      Object.keys(queryForm.value).forEach((key) => {
        queryForm.value[key] = initQueryForm()[key] || null;
      });
      initLogList();
    };
    function changePageSize(value) {
      queryForm.value.pageSize=value;
      pagination.pageSize=value;
      initLogList();
    }
    function changePage(value) {
      queryForm.value.page=value;
      pagination.page=value;
      initLogList();
    }
    const deleteAll=()=>{
      if(selectedKeys.value.length<=0){
        message.warning('请选择您要删除的数据');
        return;
      }
      dialog.warning({
        title: '警告',
        content: '你确定要删除所选的'+selectedKeys.value.length+'条数据吗?删除后无法恢复',
        positiveText: '确定',
        negativeText: '取消',
        onPositiveClick:()=>{
          const ids = selectedKeys.value.join(",");
          deleteLog({ids:ids}).then(res=>{
            if(res){
              message.success('删除成功');
              initLogList();
              selectedKeys.value=[]
            }else{
              message.error(res.message)
            }
          })
        },
        onNegativeClick: () => {
        }
      })
    };
    const clearSelect=()=>{
      selectedKeys.value=[];
    };
    const handleCheck = (keys) => {
      selectedKeys.value=keys;
    };
    async function initLogList(){
      loading.value=true;
      await logList(queryForm.value).then(res=>{
        data.value=res.records;
        pagination.itemCount=res.total;
        loading.value=false;
      })
    };
    onMounted(()=>{
      initLogList();
    })
    return {
      queryForm,
      selectedKeys,
      pagination,
      data,
      loading,
      column,
      logTypeList,
      handleSearch,
      handleReset,
      deleteAll,
      clearSelect,
      handleCheck,
      changePageSize,
      changePage
    }
  }
})
</script>

<style scoped>

</style>
